package com.lsw.jgyf.ui.fragment;

import android.text.TextUtils;

import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/7/10 0010.
 */

public class RegisterDianzhangFragmentPresenter extends BasePresenter<RegisterDianzhangFragmentContract.View> implements RegisterDianzhangFragmentContract.Presenter {

    @Inject
    HttpApi httpApi;

    @Inject
    public RegisterDianzhangFragmentPresenter() {

    }

    @Override
    public void getData() {
        mView.setData();
    }

    @Override
    public void sendCode(String phone, String type) {
        if (StringUtil.isNullOrEmpty(phone)) {
            ToastUtil.showToast(mContext, "请输入手机号！");
            return;
        }
        if (!StringUtil.checkPhoneNum(phone)) {
            ToastUtil.showToast(mContext, "请输入正确的手机号！");
            return;
        }
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .SendCode(phone, type)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                        mView.sendSucess();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    DialogProgress progress;
    boolean isdialogs;
    boolean isstart = false;

    @Override
    public void getAddress(boolean isdialog) {
        isdialogs = isdialog;
        if (progress == null)
            progress = new DialogProgress(mContext);
        if (isstart) {
            progress.show();
            return;
        }
        isstart = true;
        if (isdialog)
            progress.show();
        Disposable disposable = httpApi
                .getAddress()
                .subscribe(baseBean -> {
                    isstart = false;
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        mView.setAddress(baseBean.getData(), isdialogs);
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    isstart = false;
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }


    @Override
    public void register(String store_num, String name, String store_phone, String province_id, String city_id, String area_id, String address, String control_username, String control_mobile, String clerk_username, String clerk_mobile, String code, String password, String password_confirmation) {
        if (StringUtil.isNullOrEmpty(store_num)) {
            ToastUtil.showToast(mContext, "请输入门店编号!");
            return;
        }
        if (StringUtil.isNullOrEmpty(name)) {
            ToastUtil.showToast(mContext, "请输入门店名称!");
            return;
        }
        if (StringUtil.isNullOrEmpty(store_phone)) {
            ToastUtil.showToast(mContext, "请输入门店电话!");
            return;
        }
        if (StringUtil.isNullOrEmpty(province_id)) {
            ToastUtil.showToast(mContext, "请选择所属区域!");
            return;
        }
        if (StringUtil.isNullOrEmpty(address)) {
            ToastUtil.showToast(mContext, "请输入详细地址!");
            return;
        }
        if (StringUtil.isNullOrEmpty(control_username)) {
            ToastUtil.showToast(mContext, "请填写实际控制人名字!");
            return;
        }
        if (StringUtil.isNullOrEmpty(control_mobile)) {
            ToastUtil.showToast(mContext, "请输入实际控制人联系方式!");
            return;
        }
        if (!StringUtil.checkPhoneNum(control_mobile)) {
            ToastUtil.showToast(mContext, "请输入正确的实际控制人联系方式!");
            return;
        }
        if (StringUtil.isNullOrEmpty(clerk_username)) {
            ToastUtil.showToast(mContext, "请填写店长名字!");
            return;
        }
        if (StringUtil.isNullOrEmpty(clerk_mobile)) {
            ToastUtil.showToast(mContext, "请输入店长手机号!");
            return;
        }
        if (!StringUtil.checkPhoneNum(clerk_mobile)) {
            ToastUtil.showToast(mContext, "请输入正确的店长手机号!");
            return;
        }
        if (StringUtil.isNullOrEmpty(code)) {
            ToastUtil.showToast(mContext, "请输入验证码!");
            return;
        }
        if (StringUtil.isNullOrEmpty(password)) {
            ToastUtil.showToast(mContext, "请输入密码!");
            return;
        }
        if (StringUtil.isNullOrEmpty(password_confirmation)) {
            ToastUtil.showToast(mContext, "请输入确认密码!");
            return;
        }
        if (!TextUtils.equals(password, password_confirmation)) {
            ToastUtil.showToast(mContext, "两次密码不一致!");
            return;
        }
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .registerDianzhang(store_num, name, store_phone, province_id, city_id, area_id, address, control_username, control_mobile, clerk_username, clerk_mobile, code, password, password_confirmation)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.ID, baseBean.getData().getId() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.USERNAME, baseBean.getData().getUsername());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.MOBILE, baseBean.getData().getMobile());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STORE_ID, baseBean.getData().getStore_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.IMG_ID, baseBean.getData().getImg_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.TYPE, baseBean.getData().getType() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STATUS, baseBean.getData().getStatus() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.CREATE_TIME, baseBean.getData().getCreate_time());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.UPDATE_TIME, baseBean.getData().getUpdate_time());
                        mView.registerSucess();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }
}
